package com.example.service.admin;

import com.example.entity.Class;
import com.example.entity.DTO.ClassDetailDTO;
import com.example.entity.Department;
import com.example.exception.CustomException;
import com.example.mapper.ClassMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassService {
    @Resource
    private ClassMapper classMapper;

    public List<ClassDetailDTO> selectAllClass(String className, String majorName, String deptName) {
        if(className == null || className.equals("")) {
            className = null;
        }
        if(majorName == null || majorName.equals("")) {
            majorName = null;
        }
        if(deptName == null || deptName.equals("")) {
            deptName = null;
        }
        return classMapper.selectAllClassByNameAndMajorNameAndDeptName(className, majorName, deptName);
    }

    public void addClass(Class classData) {
        String name = classData.getName();
        Class class1 = classMapper.selectByName(name);
        if(class1 != null) {
            throw new CustomException("500", "班级名称已存在!");
        }
        classMapper.insert(classData);
    }

    public void updateClass(Class classData) {
        Integer id = classData.getId();
        Class class1 = classMapper.selectById(id);
        if(class1 == null) {
            throw new CustomException("500", "班级ID不存在!");
        }
        String dbName = class1.getName();
        String name = classData.getName();
        class1 = classMapper.selectByName(name);
        if(class1 != null && !dbName.equals(name)) {
            throw new CustomException("500", "班级名称已存在!");
        }
        classMapper.updateById(classData);
    }
}
